# Contributor: Cameron <cbanta@gmail.com>
# Maintainer: Jakub Jirutka <jakub@jirutka.cz>
pkgname=pgpool
_pkgname="$pkgname-II"
pkgver=4.5.0
pkgrel=0
pkgdesc="A connection pooling/replication server for PostgreSQL"
url="https://www.pgpool.net/"
arch="all"
license="custom"
makedepends="libpq-dev linux-headers openssl-dev>3"
pkgusers="postgres"
pkggroups="postgres"
install="$pkgname.pre-install $pkgname.post-upgrade"
subpackages="$pkgname-openrc $pkgname-doc $pkgname-static $pkgname-dev"
source="$pkgname-$pkgver.tar.gz::https://www.pgpool.net/download.php?f=$_pkgname-$pkgver.tar.gz
	$pkgname.initd
	$pkgname.confd
	$pkgname.logrotated
	"
builddir="$srcdir/$_pkgname-$pkgver"

prepare() {
	default_prepare
	update_config_sub

	# Fix listen address.
	# pgpool resolves localhost to ::1 (IPv6) and fails to bind on ::1:9999.
	sed -Ei "s/^(listen_addresses\s*=\s*)'localhost'/\1'127.0.0.1'/" \
		src/sample/pgpool.conf*
}

build() {
	./configure \
		--build=$CBUILD \
		--host=$CHOST \
		--prefix=/usr \
		--sysconfdir=/etc/$pkgname \
		--mandir=/usr/share/man \
		--infodir=/usr/share/info \
		--with-openssl \
		--disable-rpath
	make
}

check() {
	make check
}

package() {
	make DESTDIR="$pkgdir" install

	touch "$pkgdir"/etc/$pkgname/pool_passwd

	install -m755 -D "$srcdir"/$pkgname.initd "$pkgdir"/etc/init.d/$pkgname
	install -m644 -D "$srcdir"/$pkgname.confd "$pkgdir"/etc/conf.d/$pkgname
	install -m644 -D "$srcdir"/$pkgname.logrotated "$pkgdir"/etc/logrotate.d/$pkgname

	install -d -o "$pkgusers" -g "$pkggroups" -m755 "$pkgdir"/var/log/$pkgname

	install -Dm644 COPYING "$pkgdir"/usr/share/licenses/$pkgname/COPYING
}

doc() {
	local docdir="$subpkgdir"/usr/share/doc/$_pkgname
	default_doc

	mkdir -p "$docdir"
	mv "$pkgdir"/etc/$pkgname/* "$docdir"/

	local f; for f in pcp.conf pgpool.conf pool_hba.conf; do
		cp "$docdir"/$f.sample "$pkgdir"/etc/$pkgname/$f
	done
}

sha512sums="
e16bdf11fad206ab2218a55bf29ea397999db7dc8afea4732aca293d39c23aa372f9305c5d3cf4431d2a43cc65cb5f9853449579bf1811c7edcae34f703146e0  pgpool-4.5.0.tar.gz
9e54835f2a907f4b7a521d7d52cf04f69e1edfe591d9618cb2a1514953e5ba9e0d5ef543f0d6ee281fef21e64b7a6ff4a26a7f54fadbfc2d3bc7009d10dbccbf  pgpool.initd
7e605cea31ae32127ec1aca9e47a6652e87243214bde202d49a405652784e68b4dbff7d364345259d327bc37041c60c146e6f674a4aa72c6214bdba3740e700e  pgpool.confd
c9aa2ea9484ed29cb57cdff4004fa9dd4780d73c69db3378effb2e0ecd3ae178771c6a847a28e1a9cc6492ada4321584afb92c9b592119fb11898b42191f22b1  pgpool.logrotated
"
